package com.song.composejoke.ui.page

import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import android.widget.LinearLayout
import android.widget.TextView
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import coil.compose.rememberImagePainter
import coil.transform.CircleCropTransformation
import coil.transform.RoundedCornersTransformation
import com.song.composejoke.R
import com.song.composejoke.databinding.NativeViewBinding

@Composable
fun MinePage() {
    Column {
        Row(modifier = Modifier.padding(10.dp)) {
            Image(
                painter = rememberImagePainter(data = "https://t7.baidu.com/it/u=4036010509,3445021118&fm=193&f=GIF",
                    builder = {
                        transformations(CircleCropTransformation())
                    }),
                contentDescription = null, modifier = Modifier.size(128.dp)
            )
            Image(
                painter = rememberImagePainter(data = "https://t7.baidu.com/it/u=852388090,130270862&fm=193&f=GIF",
                    builder = {
                        transformations(RoundedCornersTransformation(60.0f))
                    }),
                contentDescription = null,
                modifier = Modifier
                    .size(128.dp)
            )
        }
        // Compose 中使用View
        AndroidView(factory = { context ->
            TextView(context).apply {
                layoutParams = LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT)
                layoutParams.apply { setPadding(10, 10, 10, 10) }
                text = "原生view，点击更新"
                setTextSize(TypedValue.COMPLEX_UNIT_SP, 18.0f)
                setOnClickListener(View.OnClickListener {
                    text = "更新文字" + System.currentTimeMillis()
                })
            }
        }, update = {
//            it.text = "更新文字"
        })

        AndroidView(factory = { context ->
            NativeViewBinding.inflate(LayoutInflater.from(context)).root.apply {
                val textview: TextView = findViewById(R.id.tvText)
                textview.text = "通过ViewBinding加载布局方式加载"
            }
        })


        AndroidView(factory = { context ->
            LayoutInflater.from(context).inflate(R.layout.native_view, null).apply {
                val textview: TextView = findViewById(R.id.tvText)
                textview.text = "通过最古老加载布局方式加载"
            }
        })
    }
}
